﻿package plugins.mstandio.AS3Interface.buttons{
	
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author mstandio
	 */
	public class InterfaceButton extends Sprite{		
				
		private var plain:Class;
		private var over:Class;
		private var press:Class;
		
		private var active:Boolean;		
		private var mousOver:Boolean;		
		
		private var masterMouseDown:Function;
		private var masterMouseUp:Function;
				
		private var buttonBitmap:Bitmap;		
		
		public function InterfaceButton( masterMouseDown:Function=null,  masterMouseUp:Function=null){						
			this.masterMouseUp = masterMouseUp;
			this.masterMouseDown = masterMouseDown;									
			this.buttonMode = true;
			this.useHandCursor = true;						
			this.active = false;									
			this.mousOver = false;
		}
		
		public function setBitmaps(plain:Class, over:Class = null, press:Class = null):void {
			this.plain = plain;
			this.over = over;
			this.press = press;
			
			this.replace(this.plain); // initially plain
			
			this.buildAll();
		}		
		
		private function buildAll():void {									
						
			// adding essential functionality
			this.addEventListener(MouseEvent.MOUSE_DOWN, this.masterMouseDown, false, 0, true);						
			if (this.masterMouseUp != null){
				this.addEventListener(MouseEvent.MOUSE_UP, this.masterMouseUp, false, 0, true);
				this.addEventListener(MouseEvent.MOUSE_OUT, this.masterMouseUp, false, 0, true);				
			}
			
			// adding animations			
			if (over != null){				
				this.addEventListener(MouseEvent.MOUSE_OVER, ButtonMouseOver, false, 1, true);
				this.addEventListener(MouseEvent.MOUSE_OUT, ButtonMouseOut, false, 1, true);
			}			
			if (press != null){
				this.addEventListener(MouseEvent.MOUSE_DOWN, ButtonMouseDown, false, 1, true);
				this.addEventListener(MouseEvent.MOUSE_UP, ButtonMouseUp, false, 1, true);
				if(over == null){
					this.addEventListener(MouseEvent.MOUSE_OUT, ButtonMouseOut, false, 1, true);
				}
			}			
		}			
		
		public function setActive(state:Boolean):void {							
			this.active = state;							
			if (state) {
				if (this.over != null && this.press == null) {
					this.replace(over);
				}else if (this.press != null) {
					this.replace(press);					
				}					
			}else {					
				if (this.mousOver) {
					this.replace(over);
				}else {
					this.replace(plain);				
				}				
			}			
		}		
		
		private function ButtonMouseOver(e:Event = null):void {	
			this.mousOver = true;
			if(!this.active){
				this.replace(over);
			}			
		}		
		
		private function ButtonMouseOut(e:Event = null):void {			
			this.mousOver = false;
			if(!this.active){
				this.replace(plain);
			}
		}			
		
		private function ButtonMouseDown(e:Event = null):void {
			if(!this.active){
				this.replace(press);
			}
		}		
		
		
		private function ButtonMouseUp(e:Event = null):void {
			if(!this.active){
				if(this.over!=null){
					this.replace(over);
				}else {
					this.replace(plain);
				}
			}
		}		
		
		private function replace(bitmap:Class):void {			
			if (this.buttonBitmap != null) {
				this.buttonBitmap.bitmapData = null;				
			}
			this.buttonBitmap = new Bitmap(new bitmap().bitmapData, "auto", true);													
			this.addChild(this.buttonBitmap);									
		}			
	}	
}